<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"zhengmingpei.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.8.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="程序流程中常用的基本控制结构，包括顺序执行、比较和循环。本篇介绍Shell中的使用比较的方法。 比较与测试主要使用if、ifelse和一些逻辑运算符进行比较，即： if condition;then	bye;elif condition2;then	bye2;else	bye3;fi 条件一般是由一对[  ]包裹或者一对[[  ]]包裹的部分，if和fi组成一个完整的判断逻辑。">
<meta property="og:type" content="article">
<meta property="og:title" content="LinuxShellScriptingCookbook2">
<meta property="og:url" content="https://zhengmingpei.gitee.io/2016/09/22/LinuxShellScriptingCookbook2/index.html">
<meta property="og:site_name" content="郑明培的个人博客">
<meta property="og:description" content="程序流程中常用的基本控制结构，包括顺序执行、比较和循环。本篇介绍Shell中的使用比较的方法。 比较与测试主要使用if、ifelse和一些逻辑运算符进行比较，即： if condition;then	bye;elif condition2;then	bye2;else	bye3;fi 条件一般是由一对[  ]包裹或者一对[[  ]]包裹的部分，if和fi组成一个完整的判断逻辑。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2016-09-22T07:00:00.000Z">
<meta property="article:modified_time" content="2021-11-28T03:24:42.014Z">
<meta property="article:author" content="zhengmingpei">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="Shell">
<meta property="article:tag" content="脚本">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://zhengmingpei.gitee.io/2016/09/22/LinuxShellScriptingCookbook2/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://zhengmingpei.gitee.io/2016/09/22/LinuxShellScriptingCookbook2/","path":"2016/09/22/LinuxShellScriptingCookbook2/","title":"LinuxShellScriptingCookbook2"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LinuxShellScriptingCookbook2 | 郑明培的个人博客</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">郑明培的个人博客</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives" rel="section"><i class="archive fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-about"><a href="/about" rel="section"><i class="user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories" rel="section"><i class="th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags" rel="section"><i class="tags fa-fw"></i>标签</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AF%94%E8%BE%83%E4%B8%8E%E6%B5%8B%E8%AF%95"><span class="nav-number">1.</span> <span class="nav-text">比较与测试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%95%B0%E6%AF%94%E8%BE%83"><span class="nav-number">1.1.</span> <span class="nav-text">算数比较</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%9B%B8%E5%85%B3%E6%B5%8B%E8%AF%95"><span class="nav-number">1.2.</span> <span class="nav-text">文件系统相关测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%AF%94%E8%BE%83"><span class="nav-number">1.3.</span> <span class="nav-text">字符串比较</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#test"><span class="nav-number">1.4.</span> <span class="nav-text">test</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#find%E5%91%BD%E4%BB%A4"><span class="nav-number">2.</span> <span class="nav-text">find命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%B9%E6%8D%AE%E6%96%87%E4%BB%B6%E5%90%8D%E6%9F%A5%E6%89%BE"><span class="nav-number">2.1.</span> <span class="nav-text">根据文件名查找</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E5%90%A6%E5%AE%9A%E5%8F%82%E6%95%B0%E5%92%8C%E6%8C%87%E5%AE%9A%E7%9B%AE%E5%BD%95%E6%B7%B1%E5%BA%A6"><span class="nav-number">2.1.1.</span> <span class="nav-text">使用否定参数和指定目录深度</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%89%E7%85%A7%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%E6%9F%A5%E6%89%BE"><span class="nav-number">2.2.</span> <span class="nav-text">按照文件类型查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%89%E7%85%A7%E6%97%B6%E9%97%B4%E6%9F%A5%E6%89%BE"><span class="nav-number">2.3.</span> <span class="nav-text">按照时间查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%89%E7%85%A7%E6%96%87%E4%BB%B6%E5%A4%A7%E5%B0%8F"><span class="nav-number">2.4.</span> <span class="nav-text">按照文件大小</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A9%E7%94%A8find%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%88%96%E5%8A%A8%E4%BD%9C"><span class="nav-number">2.5.</span> <span class="nav-text">利用find执行命令或动作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%92%8C-%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-number">3.</span> <span class="nav-text">%和#操作符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%9E%E8%B4%AA%E5%A9%AA%E6%93%8D%E4%BD%9C"><span class="nav-number">3.1.</span> <span class="nav-text">非贪婪操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B4%AA%E5%A9%AA%E6%93%8D%E4%BD%9C"><span class="nav-number">3.2.</span> <span class="nav-text">贪婪操作</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">zhengmingpei</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags">
        <span class="site-state-item-count">41</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/zhengmingpei" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengmingpei" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/zhengmingpei" title="gitee → https:&#x2F;&#x2F;gitee.com&#x2F;zhengmingpei" rel="noopener" target="_blank">gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://space.bilibili.com/32918983" title="bilibili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;32918983" rel="noopener" target="_blank">bilibili</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhengmingpei.gitee.io/2016/09/22/LinuxShellScriptingCookbook2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="zhengmingpei">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郑明培的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LinuxShellScriptingCookbook2
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2016-09-22 15:00:00" itemprop="dateCreated datePublished" datetime="2016-09-22T15:00:00+08:00">2016-09-22</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-28 11:24:42" itemprop="dateModified" datetime="2021-11-28T11:24:42+08:00">2021-11-28</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%9C%A8%E5%8C%97%E4%BA%AC/" itemprop="url" rel="index"><span itemprop="name">在北京</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>程序流程中常用的基本控制结构，包括顺序执行、比较和循环。本篇介绍Shell中的使用比较的方法。</p>
<h2 id="比较与测试"><a href="#比较与测试" class="headerlink" title="比较与测试"></a>比较与测试</h2><p>主要使用if、ifelse和一些逻辑运算符进行比较，即：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span> condition;</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">	<span class="built_in">bye</span>;</span><br><span class="line"><span class="keyword">elif</span> condition2;</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">	bye2;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	bye3;</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<p>条件一般是由一对<code>[  ]</code>包裹或者一对<code>[[  ]]</code>包裹的部分，<code>if</code>和<code>fi</code>组成一个完整的判断逻辑。</p>
<span id="more"></span>

<h3 id="算数比较"><a href="#算数比较" class="headerlink" title="算数比较"></a>算数比较</h3><p><strong>注意: 在[和]与操作数之间有空格，缺少会报错。</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span> [ <span class="variable">$VAR</span> -eq 0 ];</span><br><span class="line"><span class="keyword">then</span> </span><br></pre></td></tr></table></figure>
<blockquote>
<p>-gt: 大于 ;-lt: 小于 ;-eq: 等于 ;-ge: 大于等于 ;-le: 小于等于 </p>
</blockquote>
<p>多个条件进行组合：</p>
<p>逻辑与: <code>-a</code>，逻辑或：<code>-o</code>，例如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span> [ <span class="variable">$VAR</span> -gt 1 -a <span class="variable">$VAR</span> -lt 4 ];</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$VAR</span> &gt; 1 and <span class="variable">$VAR</span> &lt; 4&quot;</span>; </span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<h3 id="文件系统相关测试"><a href="#文件系统相关测试" class="headerlink" title="文件系统相关测试"></a>文件系统相关测试</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">[ -f <span class="variable">$file_var</span> ] <span class="comment"># 给定变量包含正常文件路径或文件名，则返回真。</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>-x: 文件可执行； -d: 是目录；-e: 文件存在；-L: 是符号链接；-w: 文件可写；-r: 可读</p>
</blockquote>
<h3 id="字符串比较"><a href="#字符串比较" class="headerlink" title="字符串比较"></a>字符串比较</h3><p>字符串的比较，书中推荐使用双<code>[]</code>，即：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">[[ <span class="variable">$str1</span> = <span class="variable">$str2</span> ]]</span><br><span class="line">[[ <span class="variable">$str1</span> != <span class="variable">$str2</span> ]]</span><br><span class="line">[[ <span class="variable">$str1</span> &gt; <span class="variable">$str2</span> ]] <span class="comment">#字母序大</span></span><br><span class="line">[[ <span class="variable">$str1</span> &lt; <span class="variable">$str2</span> ]]</span><br><span class="line">[[ -z <span class="variable">$str1</span> ]] <span class="comment">#如果包含空字符串，返回真</span></span><br><span class="line">[[ -n <span class="variable">$str1</span> ]] <span class="comment">#如果包含非空字符串，返回真</span></span><br></pre></td></tr></table></figure>

<p>逻辑运算使用<code>&amp;&amp;</code>和<code>||</code>:</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span> [[ -n  <span class="variable">$str1</span> ]] &amp;&amp; [[ -z <span class="variable">$str2</span> ]] <span class="comment">#str1非空而且str2是空的</span></span><br></pre></td></tr></table></figure>

<h3 id="test"><a href="#test" class="headerlink" title="test"></a>test</h3><p><code>test</code>等价于<code>[]</code>，比如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span> <span class="built_in">test</span> <span class="variable">$var</span> -eq 0;</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;ture&quot;</span>;</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<h2 id="find命令"><a href="#find命令" class="headerlink" title="find命令"></a>find命令</h2><p>find可以遍历查找符合要求的文件。</p>
<h3 id="根据文件名查找"><a href="#根据文件名查找" class="headerlink" title="根据文件名查找"></a>根据文件名查找</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . -name <span class="string">&quot;*.txt&quot;</span> <span class="comment"># 查找当前目录下的txt文件</span></span><br><span class="line">find . -iname <span class="string">&quot;*.txt&quot;</span> <span class="comment"># 忽略大小写，查找TXT和txt文件</span></span><br><span class="line">find . \( -name <span class="string">&quot;*.txt&quot;</span> -o -name <span class="string">&quot;*.pdf&quot;</span> \) -<span class="built_in">print</span> <span class="comment"># 查找txt和pdf文件 </span></span><br></pre></td></tr></table></figure>

<p>使用正则表达式匹配：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . -regex <span class="string">&quot;.*\(\.py\|\.sh\)$&quot;</span> <span class="comment">#查找py和sh文件</span></span><br></pre></td></tr></table></figure>

<h4 id="使用否定参数和指定目录深度"><a href="#使用否定参数和指定目录深度" class="headerlink" title="使用否定参数和指定目录深度"></a>使用否定参数和指定目录深度</h4><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . ! -name <span class="string">&quot;*.txt&quot;</span> -<span class="built_in">print</span> <span class="comment"># 匹配所有不以.txt结尾的文件名</span></span><br><span class="line">find . -maxdepth 1 -name <span class="string">&quot;f*&quot;</span> -<span class="built_in">print</span> <span class="comment"># 只匹配当前目录下以f开头的文件</span></span><br></pre></td></tr></table></figure>

<h3 id="按照文件类型查找"><a href="#按照文件类型查找" class="headerlink" title="按照文件类型查找"></a>按照文件类型查找</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . -<span class="built_in">type</span> d -<span class="built_in">print</span> <span class="comment"># 目录</span></span><br><span class="line">find . -<span class="built_in">type</span> f -<span class="built_in">print</span> <span class="comment"># 普通文件</span></span><br><span class="line">find . -<span class="built_in">type</span> s -<span class="built_in">print</span> <span class="comment"># 套接字</span></span><br></pre></td></tr></table></figure>

<h3 id="按照时间查找"><a href="#按照时间查找" class="headerlink" title="按照时间查找"></a>按照时间查找</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . -<span class="built_in">type</span> f -atime -7 -<span class="built_in">print</span> <span class="comment"># 最近7天访问过的文件</span></span><br><span class="line">find . -<span class="built_in">type</span> f -atime 7 -<span class="built_in">print</span> <span class="comment"># 恰好7天前访问过的文件</span></span><br><span class="line">find . -<span class="built_in">type</span> f -atime +7 -<span class="built_in">print</span> <span class="comment"># 7天以上时间之前访问过的文件</span></span><br><span class="line">find . -<span class="built_in">type</span> f -mtime -7 -<span class="built_in">print</span> <span class="comment"># 最近7天修改过的文件</span></span><br><span class="line">find . -<span class="built_in">type</span> f -ctime -7 -<span class="built_in">print</span> <span class="comment"># 最近7天文件元数据(权限或所有权)改动过的文件</span></span><br></pre></td></tr></table></figure>

<h3 id="按照文件大小"><a href="#按照文件大小" class="headerlink" title="按照文件大小"></a>按照文件大小</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . -<span class="built_in">type</span> f -size +2M <span class="comment"># 大于2M的文件</span></span><br></pre></td></tr></table></figure>

<h3 id="利用find执行命令或动作"><a href="#利用find执行命令或动作" class="headerlink" title="利用find执行命令或动作"></a>利用find执行命令或动作</h3><p>主要使用<code>-exec</code>：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">find . -<span class="built_in">type</span> f -mtime +10 -name <span class="string">&quot;*.txt&quot;</span> -<span class="built_in">exec</span> cp &#123;&#125; OLD \;</span><br><span class="line"><span class="comment"># 查找10天前的txt文件，并复制到OLD目录中。</span></span><br></pre></td></tr></table></figure>

<h2 id="和-操作符"><a href="#和-操作符" class="headerlink" title="%和#操作符"></a>%和#操作符</h2><p>从”名称.扩展名”的格式中提取文名称或者扩展名，需要使用<code>%</code>和<code>#</code>：</p>
<h3 id="非贪婪操作"><a href="#非贪婪操作" class="headerlink" title="非贪婪操作"></a>非贪婪操作</h3><p><code>$&#123;VAR%.*&#125;</code>，即可得到从<code>$VAR</code>中删除位于<code>%</code>右侧匹配的字符而最终得到的字符串，从右往左匹配。例如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">file=<span class="string">&quot;hi.txt&quot;</span></span><br><span class="line">name=<span class="variable">$&#123;file%.*&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$name</span></span><br></pre></td></tr></table></figure>

<p>输出为：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">hi</span><br></pre></td></tr></table></figure>

<p>同理，使用<code>&#123;$VAR#*.&#125;</code>，即可得到从<code>$VAR</code>中删除位于<code>#</code>右侧匹配的字符而最终得到的字符串，从左往右匹配。例如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">file=<span class="string">&quot;hi.txt&quot;</span></span><br><span class="line">name=<span class="variable">$&#123;file#*.&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$name</span></span><br></pre></td></tr></table></figure>

<p>输出为：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">txt</span><br></pre></td></tr></table></figure>

<p>仔细想想，这两个操作符一个是从右往左(%)，一个是从左到右(#)，都是删掉原始字符串中匹配到的部分得到想要的部分。</p>
<h3 id="贪婪操作"><a href="#贪婪操作" class="headerlink" title="贪婪操作"></a>贪婪操作</h3><p>上面的叫做非贪婪操作，原因是它们都只能按照顺序找到匹配通配符的最短结果进行删除。</p>
<p>比如对于<code>hack.tar.gz</code>，使用<code>%</code>和<code>#</code>进行提取时，将会得到<code>hack.tar</code>和<code>tar.gz</code>。</p>
<p>而使用<code>%%</code>和<code>##</code>进行贪婪匹配，得到的结果就是<code>hack</code>和<code>gz</code>了。</p>
<p>合理对以上的几种操作符进行组合，可以完成很多有用的筛选:</p>
<p>比如对于tar.gz文件，用<code>#</code>可以提取扩展名，用<code>%%</code>可以提取文件名。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Linux/" rel="tag"># Linux</a>
              <a href="/tags/Shell/" rel="tag"># Shell</a>
              <a href="/tags/%E8%84%9A%E6%9C%AC/" rel="tag"># 脚本</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2016/09/21/MySQL-Note2/" rel="prev" title="MySQL学习笔记2">
                  <i class="fa fa-chevron-left"></i> MySQL学习笔记2
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2016/09/27/MySQL-Note3/" rel="next" title="MySQL学习笔记3">
                  MySQL学习笔记3 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zhengmingpei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
